package com.example.mywanandroid.home.ui;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.bumptech.glide.Glide;
import com.example.mywanandroid.R;
import com.example.mywanandroid.home.HomeRecyclerAdapter;
import com.example.mywanandroid.home.RecyclerViewItem;
import com.example.mywanandroid.home.ViewPagerAdatper;
import com.example.mywanandroid.home.ViewPagerItem;
import com.example.mywanandroid.home.WebViewActivity;
import com.example.mywanandroid.home.business.GetData;
import com.example.mywanandroid.home.business.ParseData;
import com.example.mywanandroid.home.business.RecyclerViewCallBack;
import com.example.mywanandroid.home.business.ViewPagerCallBack;
import com.example.mywanandroid.util.GlobalLoading;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.header.BezierRadarHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

public class InitComponents {
    private static int currentPage = 1;
    private HomeRecyclerAdapter homeRecyclerAdapter;
    private ViewPagerAdatper viewPagerAdatper;

    // 初始化ViewPager界面
    public void initViewPager(ViewPager viewPager, final List<ViewPagerItem> data, final FragmentActivity currentActivity) {
        viewPagerAdatper = new ViewPagerAdatper(data, currentActivity);
        viewPager.setAdapter(viewPagerAdatper);
        viewPager.setCurrentItem(0);
//        viewPager.setAdapter(pagerAdapter);
//        viewPager.setCurrentItem(1000 * viewList.size());
    }

    // 初始化RecyclerView界面
    public void initRecyclerView(RecyclerView recyclerView, final List<RecyclerViewItem> recList, final FragmentActivity currentActivity) {
        homeRecyclerAdapter = new HomeRecyclerAdapter(currentActivity, recList);
        homeRecyclerAdapter.setOnItemClickListener(new HomeRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                String link = recList.get(position).getLink();
                Intent intent = new Intent(currentActivity, WebViewActivity.class);
                intent.putExtra("link", link);
                currentActivity.startActivity(intent);
            }
        });
        recyclerView.setLayoutManager(new LinearLayoutManager(currentActivity, LinearLayoutManager.VERTICAL, false));
        recyclerView.addItemDecoration(new DividerItemDecoration(currentActivity, DividerItemDecoration.VERTICAL));
        recyclerView.setAdapter(homeRecyclerAdapter);
    }

    // 初始化下拉刷新功能
    public void initRefresh(RefreshLayout refreshLayout, final FragmentActivity currentActivity) {
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                GetData.getRecyclerViewData(0, new RecyclerViewCallBack() {
                    @Override
                    public void onGetRecyclerViewData(String rawData) {
                        if (rawData == null) {
                            currentActivity.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast toast = Toast.makeText(currentActivity, "请求失败，显示为缓存数据", Toast.LENGTH_SHORT);
                                    toast.show();
                                }
                            });
                        }
                        final List<RecyclerViewItem> extraList = ParseData.parseRecyclerViewData(rawData);
                        currentActivity.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                homeRecyclerAdapter.refresh(extraList);
                            }
                        });
                    }
                });
                GetData.getViewPagerData(new ViewPagerCallBack() {
                    @Override
                    public void onGetViewPagerData(String rawData) {
                        final List<ViewPagerItem> extraList = ParseData.parseViewPagerData(rawData);
                        currentActivity.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                viewPagerAdatper.refresh(extraList);
                            }
                        });
                    }
                });
                refreshLayout.finishRefresh(200/*, false*/);// 传入false表示刷新失败
            }
        });
        //设置 Header 为 贝塞尔雷达样式
        refreshLayout.setRefreshHeader(new BezierRadarHeader(currentActivity).setEnableHorizontalDrag(true));
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull final RefreshLayout refreshLayout) {
                GetData.getRecyclerViewData(currentPage++, new RecyclerViewCallBack() {
                    @Override
                    public void onGetRecyclerViewData(String rawData) {
                        final List<RecyclerViewItem> extraList = ParseData.parseRecyclerViewData(rawData);
                        currentActivity.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                homeRecyclerAdapter.add(extraList);
                            }
                        });
                    }
                });
                GetData.getViewPagerData(new ViewPagerCallBack() {
                    @Override
                    public void onGetViewPagerData(String rawData) {
                        final List<ViewPagerItem> extraList = ParseData.parseViewPagerData(rawData);
                        currentActivity.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                viewPagerAdatper.refresh(extraList);
                            }
                        });
                    }
                });
                GlobalLoading globalLoading = new GlobalLoading(currentActivity);
                refreshLayout.finishLoadMore(true);//加载完成
            }
        });
    }

    // 初始化点击收藏
    public static void initCollectImageView(final ImageView imageView) {
        // 图片表
        final Integer[] imageList = {R.drawable.ic_vector_gray_heart, R.drawable.ic_vector_red_heart};
        final Integer[] curPic = {0};
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(curPic[0] == 0) {
                    imageView.setImageResource(imageList[1]);
                    collectAnimator(imageView);
                    curPic[0] = 1;
                }
                else {
                    imageView.setImageResource(imageList[0]);
                    curPic[0] = 0;
                }
            }
        });
    }
    // 加入动画
    private static void collectAnimator(ImageView imageView) {
        ObjectAnimator translationY = ObjectAnimator.ofFloat(
                imageView, "translationY",
                imageView.getTranslationY(),
                imageView.getTranslationY() - 40f,
                imageView.getTranslationY()
        );
        ObjectAnimator scaleX = ObjectAnimator.ofFloat(
                imageView, "scaleX",
                1f, 2f, 1f
        );
        ObjectAnimator scaleY = ObjectAnimator.ofFloat(
                imageView, "scaleY",
                1f, 2f, 1f
        );
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.playTogether(scaleX, scaleY, translationY);
        animatorSet.start();
    }
}